package com.sq.common.utils;

import com.sq.common.ana.Param;
import com.sq.common.enums.ReqTypeEnum;
import com.sq.common.exception.param.ParamNotComplete;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static org.apache.commons.lang3.StringUtils.isBlank;

/**
 * @Author Li sir
 * @Date 2024/10/16
 */
public class ParamUtil {

    public static <T> void checkParam(T t, ReqTypeEnum reqTypeEnum){

        Class<?> tClass = t.getClass();
        Map<Field, Param> map = getAllField(tClass);

        if (map.isEmpty()){
            return;
        }
        Set<Field> set = map.keySet();
        for (Field f : set) {
            try {
                f.setAccessible(true);
                Object o = f.get(t);

                Param annotation =  map.get(f);
                ReqTypeEnum[] condition = annotation.condition();
                // 查看 reqTypeEnum 是否在配置的  condition 中
                boolean inReqTypeEnum = isInReqTypeEnum(condition, reqTypeEnum);

                // 值为空，并且不在范围内
                if (Objects.isNull(o) && !inReqTypeEnum){
                    continue;
                }


                //值为空，并且在范围内
                if (Objects.isNull(o) && inReqTypeEnum){
                    throw new ParamNotComplete(annotation.tip());

                }

                // 值不为空，并且在范围内 并且配置了正则表达式
                if (!Objects.isNull(o) && inReqTypeEnum && StringUtils.isNotEmpty(annotation.regular())){
                    // todo 进行 正则匹配，看匹配是否正确
                }
            }
            catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }catch (ParamNotComplete e){
                throw new ParamNotComplete(e.getMessage());
            } catch (Exception e){
                throw new RuntimeException(e.getMessage());
            }
        }


    }


    public static <T> void checkParam(Class<T> tClass, ReqTypeEnum reqTypeEnum){

        Map<Field, Param> map = getAllField(tClass);

        if (map.isEmpty()){
            return;
        }
        Set<Field> set = map.keySet();
        for (Field f : set) {
            try {
                f.setAccessible(true);
                Object o = f.get(tClass);

                Param annotation =  map.get(f);
                ReqTypeEnum[] condition = annotation.condition();
                // 查看 reqTypeEnum 是否在配置的  condition 中
                boolean inReqTypeEnum = isInReqTypeEnum(condition, reqTypeEnum);

                // 值为空，并且不在范围内
                if (Objects.isNull(o) && !inReqTypeEnum){
                    continue;
                }


                //值为空，并且在范围内
                if (Objects.isNull(o) && inReqTypeEnum){
                    throw new ParamNotComplete(annotation.tip());
                }

                // 值不为空，并且在范围内 并且配置了正则表达式
                if (!Objects.isNull(o) && inReqTypeEnum && StringUtils.isNotEmpty(annotation.regular())){
                    // todo 进行 正则匹配，看匹配是否正确
                }
            }
            catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }catch (Exception e){

            }
        }


    }

    public static Map<Field,Param> getAllField(Class c){
        Map<Field,Param> map = new HashMap<>();
        //所有字段
        Field[] allField = c.getDeclaredFields();
        for (Field field : allField) {
            Param annotation = field.getAnnotation(Param.class);
            if (!Objects.isNull(annotation)){
                map.put(field,annotation);
            }
        }
        return map;
    }


    public static boolean isInReqTypeEnum(ReqTypeEnum[] condition,ReqTypeEnum reqTypeEnum){

        for (ReqTypeEnum r : condition){
            if (r.name().equals(reqTypeEnum.name())){
                return true;
            }
        }
        return false;

    }

}
